<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2019/2/27
 * Time: 15:02
 */

use vod\Request\V20170321 as vod;
use OSS\OssClient;
use OSS\Core\OssUtil;

class MyUtils
{
    public function __construct($apiRegionId = null)
    {
        $this->accessKeyId                  = 'LTAILIq3mC7PTazx';
        $this->accessKeySecret              = 'a8xQpLiiRue3FtPd8pOCBqq9EZhFwz';
        $this->cateid_video_lesson          = 1000019933;
        $this->template_video_groupId       = 'b32a8cb570459e28236533d0601236f8';
        $this->cateid_img                   = 1000025123;

        if (empty($apiRegionId)) {
            // VoD的接入地址，国内为cn-shanghai，参考：https://help.aliyun.com/document_detail/98194.html
            $this->apiRegionId = 'cn-shanghai';
        } else {
            $this->apiRegionId = $apiRegionId;
        }

        // VOD和OSS的连接超时时间，单位秒
        $this->connTimeout = 1;

        // 设置VOD请求超时时间，单位秒
        $this->vodTimeout = 3;

        // 设置OSS请求超时时间，单位秒，默认是7天, 建议不要设置太小，如果上传文件很大，消耗的时间会比较长
        $this->ossTimeout = 86400*7;

        // 失败后的最大重试次数
        $this->maxRetryTimes = 3;

        $this->multipartThreshold = 10 * 1024 * 1024;
        $this->multipartPartSize = 10 * 1024 * 1024;
        $this->multipartThreadsNum = 3;

        $this->vodClient = $this->initVodClient();
        $this->ossClient = null;
        $this->ecsRegionId = null;
        $this->enableSSL = false;
    }

    // 上传视频至阿里云vod
    public function upload_local_video($filePath, $title)
    {
        $uploader = new \AliyunVodUploader($this->accessKeyId, $this->accessKeySecret);
        $uploadVideoRequest = new \UploadVideoRequest($filePath, $title);
        $uploadVideoRequest->setCateId($this->cateid_video_lesson);
        $uploadVideoRequest->setTemplateGroupId($this->template_video_groupId);
        $videoId = $uploader->uploadLocalVideo($uploadVideoRequest);
        return $videoId;
    }

    // 上传图片至阿里云vod
    public function upload_local_img($filePath, $title)
    {
        $uploader = new \AliyunVodUploader($this->accessKeyId, $this->accessKeySecret);
        $uploadImageRequest = new \UploadImageRequest($filePath, $title);
        $uploadImageRequest->setCateId($this->cateid_img);
        $res = $uploader->uploadLocalImage($uploadImageRequest);
        return $res;
    }

    // 获取视频播放信息
    public function get_video_playinfo($video_id)
    {
        if ($video_id) {
            $playInfo = $this->getPlayInfo($this->vodClient, $video_id);
            if (is_string($playInfo)) {
                return $playInfo;
            }
            return $playInfo->PlayInfoList->PlayInfo;
        }
        return [];
    }

    // 获取视频获取播放凭证
    public function get_video_playAuth($video_id)
    {
        if ($video_id) {
            $playAuth = $this->getPlayAuth($this->vodClient, $video_id);
            return $playAuth->PlayAuth;
        }
        return [];
    }

    // 获取图片
    public function get_img_url($img_id)
    {
        if ($img_id) {
            $imgInfo = $this->getImgUrl($this->vodClient, $img_id);
            if (is_string($imgInfo)) {
                return $imgInfo;
            }
            return $imgInfo->ImageInfo->Mezzanine;
        }
        return '';
    }

    // 获取视频播放信息
    public function del_video($video_id)
    {
        if ($video_id) {

            $request = new vod\DeleteVideoRequest();
            $request->setVideoIds($video_id);   // 支持批量删除视频；videoIds为传入的视频ID列表，多个用逗号分隔
            $request->setAcceptFormat('JSON');
            return $this->vodClient->getAcsResponse($request);
        }
        return [];
    }

    public function del_img($img_id)
    {
        if ($img_id) {
            $request = new vod\DeleteImageRequest();
            $request->setDeleteImageType("ImageId");
            $request->setImageIds($img_id);   // 支持批量删除视频；videoIds为传入的视频ID列表，多个用逗号分隔
            return $this->vodClient->getAcsResponse($request);
        }
        return [];
    }

    private function initVodClient()
    {
        HttpHelper::$connectTimeout = $this->connTimeout;
        HttpHelper::$readTimeout = $this->vodTimeout;
        $profile = DefaultProfile::getProfile($this->apiRegionId, $this->accessKeyId, $this->accessKeySecret, $this->securityToken);
        return new DefaultAcsClient($profile);
    }

    private function getPlayInfo($client, $videoId) {
        $request = new vod\GetPlayInfoRequest();
        $request->setVideoId($videoId);
        $request->setFormats('mp4,mp3');
        $request->setOutputType('cdn');
        $request->setAuthTimeout(3600);
        $request->setAcceptFormat('JSON');
        try {
            return $client->getAcsResponse($request);
        } catch (ClientException $err) {
            return $err->getErrorMessage();
        }
    }

    private function getPlayAuth($client, $videoId) {
        $request = new vod\GetVideoPlayAuthRequest();
        $request->setVideoId($videoId);
        $request->setAuthInfoTimeout(120);
        $request->setAcceptFormat('JSON');
        return $client->getAcsResponse($request);
    }

    private function getImgUrl($client, $img_id) {
        $request = new vod\GetImageInfoRequest();
        $request->setImageId($img_id);
        $request->setAuthTimeout(PHP_INT_MAX);
        try {
            return $client->getAcsResponse($request);
        } catch (ClientException $err) {
            return $err->getErrorMessage();
        }
    }
}